package proj1.ui.component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.swing.table.AbstractTableModel;

import proj1.dao.Radiator;
import proj1.dao.Vendor;
import proj1.dao.Radiator.SIZE;
import proj1.util.Logger;

public class RadiatorTableModel extends AbstractTableModel {
	private static Logger log = Logger.getLogger(RadiatorTableModel.class);
	private List<List<String>> radiatorData = new ArrayList<>();
	private List<String> colnames = Arrays.asList("Size", "Vendor", "Part Number", "Price", "In Stock");
	
	public void updateTable(Map<SIZE, Map<Vendor, Set<Radiator>>> data){
		radiatorData.clear();
		for(Entry<SIZE,Map<Vendor,Set<Radiator>>> vFs : data.entrySet()){
			SIZE size = vFs.getKey();
			for(Entry<Vendor,Set<Radiator>> rFv : vFs.getValue().entrySet()){
				Vendor vendor = rFv.getKey();
				for(Radiator radiator : rFv.getValue()){
					radiatorData.add(Arrays.asList(
						size.toString(),
						vendor.toString(),
						radiator.getPartNumber(),
						String.valueOf(radiator.getPrice()),
						String.valueOf(radiator.getQuantity())));
				}
			}	
		}
		fireTableDataChanged();
		log.info("Table updated. " + radiatorData.size() + " results");
	}
	public void clearTable(){
		radiatorData.clear();
		fireTableDataChanged();
	}
	@Override
	public String getColumnName(int columnIndex) {
		return colnames.get(columnIndex);
	}
	@Override
	public int getColumnCount() {
		return colnames.size();
	}

	@Override
	public int getRowCount() {
		return radiatorData.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return radiatorData.get(rowIndex).get(columnIndex);
	}

}
